import { reactive, ref } from "vue";
import { deleteCourseAPI, getCourseListAPI } from "@/api/resource.ts";
import { ElMessage } from "element-plus";

export default function useCourse() {

  const courseList = ref()
  const listDialog = ref()
  const dialog = ref()
  const query = reactive({
    type: '1',
    pager: 1,
    limit: 10
  })

  // 获取课程列表
  const getCourseList = async () => {
    const res = await getCourseListAPI(query)
    courseList.value = res.data
  }

  const addCourse = async () => {
    dialog.value.init({}, 1)
  }

  // 编辑课程
  const EditCourse = (row: any) => {
    dialog.value.init(row, 2)
  }

  // 查看报名
  const handleLook = (row: any) => {
    listDialog.value.init(row)
  }

  // 删除课程
  const deleteCourse = async (id: string) => {
    const res: any = await deleteCourseAPI(id)
    if (res.code === 0) {
      ElMessage({
        type: 'success',
        message: res.message
      })
      await getCourseList()
    }
  }

  return {
    dialog,
    listDialog,
    courseList,
    addCourse,
    getCourseList,
    EditCourse,
    handleLook,
    deleteCourse
  }
}
